﻿// JavaScript Document

var validate = function () {
    jQuery.validator.addMethod("ssaRemote", function (value, element, param) {
        if (this.optional(element)) {
            return "dependency-mismatch";
        }

        var previous = this.previousValue(element);
        if (!this.settings.messages[element.name]) {
            this.settings.messages[element.name] = {};
        }
        previous.originalMessage = this.settings.messages[element.name].remote;
        this.settings.messages[element.name].remote = previous.message;

        param = typeof param === "string" && { url: param } || param;

        if (previous.old === value) {
            return previous.valid;
        }

        previous.old = value;
        var validator = this;
        this.startRequest(element);
        var data = {};
        data[element.name] = value;
        $.ajax($.extend(true, {
            url: param,
            mode: "abort",
            port: "validate" + element.name,
            dataType: "json",
            data: data,
            success: function (response) {
                validator.settings.messages[element.name].remote = previous.originalMessage;
                var valid = response.Status === true || response.Status === "true";
                if (valid) {
                    var submitted = validator.formSubmitted;
                    validator.prepareElement(element);
                    validator.formSubmitted = submitted;
                    validator.successList.push(element);
                    delete validator.invalid[element.name];
                    validator.showErrors();
                } else {
                    var errors = {};
                    var message = response.Description || validator.defaultMessage(element, "ssaRemote");
                    $.validator.messages["ssaRemote"] = response.Description;
                    errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
                    validator.invalid[element.name] = true;
                    validator.showErrors(errors);
                }
                previous.valid = valid;
                validator.stopRequest(element, valid);
            }
        }, param));
        return "pending";
    });

    // 不包含空格   
    jQuery.validator.addMethod("notSpace", function (value, element) {
        var space = /^[^\s]*$/;
        return this.optional(element) || (space.test(value));
    }, "");

    return {
        init: function (id, rules, messages) {
            var _v = $("#" + id).validate({
                //onsubmit: false,
                errorElement: "div",
                errorClass: "wrong",
                errorPlacement: function (error, element) {
                    error.appendTo(element.closest("div.col-right"));
                },
                onfocusout: function (element) { $(element).valid(); },
                //onkeyup: function(element) { $(element).valid(); },
                onkeyup: false,
                rules: rules,
                messages: messages
            });
            return _v;
        }
    };
}();